<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use app\common\model\order\Goods as Ordergoods;
use app\common\model\order\Order;
use app\common\model\user\Level;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {
        }
        $levels = Level::alias('l')->join('fa_user u', "u.level_id=l.id", 'left')->group('l.id')->field('l.name,count(u.id) AS num')->select();
        $c1order = Order::where('order_status', '>', '0')->where('order_class', '1')->column('id');
        $c1orders =  Ordergoods::where('order_id', 'in', $c1order)->sum('goods_num');

        $c2order = Order::where('order_status', '>', '0')->where('order_class', '2')->column('id');
        $c2orders =  Ordergoods::where('order_id', 'in', $c2order)->sum('goods_num');


        $c3order = Order::where('order_status', '>', '0')->where('order_class', '3')->column('id');
        $c3orders =  Ordergoods::where('order_id', 'in', $c3order)->sum('goods_num');

        
        $c4order = Order::where('order_status', '>', '0')->where('order_class', '4')->column('id');
        $c4orders =  Ordergoods::where('order_id', 'in', $c4order)->sum('goods_num');

        $this->view->assign([
            'totaluser'         => User::count(),
            'totalscore'         => User::sum('score'),
            'totalmoney'         => User::sum('money'),
            'totaldividend'         => User::sum('dividend'),
            // 'totalconsume'         => Order::where('order_class', '1')->where('order_status', 'in', ['10', '20', '30', '40'])->sum('total_fee'),
            'totalconsume'         => User::sum('consume'),

            'totalclass2money'         => Order::where('order_class', '2')->where('order_status', 'in', ['10', '20', '30', '40'])->sum('total_fee'),
            'totalclass3money'         => Order::where('order_class', '3')->where('order_status', 'in', ['10', '20', '30', '40'])->sum('total_fee'),
            'totalclass4money'         => Order::where('order_class', '4')->where('order_status', 'in', ['10', '20', '30', '40'])->sum('total_fee'),


            'c1orders' => $c1orders,
            'c2orders' => $c2orders,
            'c3orders' => $c3orders,
            'c4orders' => $c4orders,


            // 'totaluser'         => User::count(),

            'levels' => $levels
            // 'attachmentnums'    => Attachment::count(),
            // 'attachmentsize'    => Attachment::sum('filesize'),
            // 'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            // 'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
        ]);

        // $this->assignconfig('column', array_keys($userlist));
        // $this->assignconfig('userdata', array_values($userlist));

        return $this->view->fetch();
    }
}
